﻿using UnityEngine;
using System.Collections;

[ExecuteInEditMode]
public class MeTexture : UITexture
{

		private MeRetain mTextureRetain;
		public static InstanceCallBackAsyn<Texture, string> InstanceTextureCallBackAsyn;
		private CallBackData<Texture> mCallBackData;
		private CallBackData<Texture> mCallBackDataAsyn;
		private InstanceCallBack<Texture, string> mInstanceCallBack;

		public void LoadTexture (string url)
		{
				if (mTextureRetain != null) {
						mTextureRetain.Release ();
				}

				if (mCallBackData == null) {
						mCallBackData = (texture) => {
								this.mainTexture = texture;};

//						mCallBackDataAsyn = (texture) => {
//								StartCoroutine (MainTextureAsyn (texture));};

						mCallBackDataAsyn = mCallBackData;

						mInstanceCallBack = (data, callBackData) => {
								StartCoroutine (InstanceTextureCallBackAsyn (data, callBackData));};
				}
		
				mTextureRetain = MeCache.Shared ().RetainData<Texture> (url, mCallBackData, mCallBackDataAsyn, mInstanceCallBack);
		}

		private IEnumerator MainTextureAsyn (Texture texture)
		{
				yield return 0;
				this.mainTexture = texture;
		}
		
		protected override void OnDestroy ()
	    {		
				if (mTextureRetain != null) {
						mTextureRetain.Release ();
				}

				base.OnDestroy ();
		}


}
